package com.mini.rpc.provider.registry;

import com.mini.rpc.common.ServiceMeta;

import java.io.IOException;

public interface RegistryService {

    /**
     * 获取注册中心的名字
     */
    default String getRegistryName() {
        String simpleName = this.getClass().getSimpleName();
        StringBuilder stringBuilder = new StringBuilder();
        int count = 0;
        for (int i = 0; i < simpleName.length(); i++) {
            // 快速判断一个字母为大写的工具类
            if (Character.isUpperCase(simpleName.charAt(i))) {
                count++;
                if (count == 2) {
                    break;
                }
            }
            stringBuilder.append(simpleName.charAt(i));

        }
        return stringBuilder.toString();
    }

    /**
     * 服务注册
     */
    void register(ServiceMeta serviceMeta) throws Exception;

    /**
     * 服务注销
     */
    void unRegister(ServiceMeta serviceMeta) throws Exception;

    /**
     * 服务发现
     */
    ServiceMeta discovery(String serviceName, int invokerHashCode) throws Exception;

    /**
     * 服务发现, 并提供负载均衡算法
     */
    ServiceMeta discovery(String serviceName, String loadBalancer, int invokerHashCode) throws Exception;

    /**
     * 注册中心销毁
     */
    void destroy() throws IOException;
}
